﻿using Red.EngineData.View;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Red.Controls.Shell;

namespace Red.Controls.ViewModels
{
    /// <summary>
    /// Animation item in animation list.
    /// </summary>
    public class AnimationListItem : TreeItemEngine
    {
        private static Canvas _icon = RedApplication.FindCanvasResource( "appbar_man_walk" );

        /// <summary>
        /// Constructs.
        /// </summary>
        public AnimationListItem( AnimationList parent, DataView view, string title, Action<TreeItemEngine> onSynchronized, bool enumerateChildren = true )
            : base( parent, title, null, view, _icon, onSynchronized, String.Empty, enumerateChildren )
        {
            View.Execute( Interop.Make( ".name" ), result => { Title = result.Data; }, ex => { HandleError( "invalid" ); } );
        }

        /// <summary>
        /// Select given animation item.
        /// </summary>
        public void Select()
        {
            Execute( "select" );
        }

        /// <summary>
        /// Show errir in UI.
        /// </summary>
        private void HandleError( string errorMessage )
        {
            Title = errorMessage;
        }
    }

    /// <summary>
    /// Selectable animation list for only one animation set.
    /// </summary>
    public class AnimationList : AnimationListItem
    {
        /// <summary>
        /// Constructs.
        /// </summary>
        public AnimationList( DataView view, Action<TreeItemEngine> onSynchronized, bool enumerateChildren = true )
            : base( null, view, "Animations", onSynchronized, enumerateChildren )
        {
        }

        protected override RedTreeItem CreateTreeItem( DataView childView )
        {
            return new AnimationListItem( this, childView, "Animation", null, false );
        }
    }
}
